package com.cd.model;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;

import com.cd.util.DBUtil;

public class User {
	private int id;
	private String username="";
	private String password="";
	private String nickname="";
	private String email="";
	private String number="";
	private String sex="未知";
	private Timestamp date;
	
	
	
	public User()
	{
		super();
		username = "";
		password = "";
		nickname = "";
		email = "";
		number = "";
		sex = "未知";
		date = new Timestamp(System.currentTimeMillis());
	}
	public Timestamp getDate() {
		return date;
	}
	public void setDate(Timestamp date) {
		this.date = date;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getNickname() {
		return nickname;
	}
	public void setNickname(String nickname) {
		this.nickname = nickname;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", username=" + username + ", password="
				+ password + ", nickname=" + nickname + ", email=" + email
				+ ", number=" + number + ", sex=" + sex + "]";
	}
	
	public int addUser() throws SQLException{
		DBUtil db =new DBUtil();
		if(this.nickname==""){
			this.nickname = this.username;
		}
		/*String sql = "INSERT INTO user (username,password,nickname,email,number,sex,date) VALUES (?,?,?,?,?,?,?)";		
		PreparedStatement preparedStatement = db.getConnection().prepareStatement(sql);
		preparedStatement.setString(1, this.username);
		preparedStatement.setString(2, this.password);
		preparedStatement.setString(3,this.nickname);
		preparedStatement.setString(4, this.email==null?"":this.email);
		preparedStatement.setString(5, this.number==null?"":this.number);
		preparedStatement.setString(6, this.sex);
		preparedStatement.setTimestamp(7,Timestamp.valueOf(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").
		format(new Date(System.currentTimeMillis()))));*/
		
		
		String sql = "INSERT INTO user (username,password,nickname,email,number,sex) VALUES (?,?,?,?,?,?)";		
		PreparedStatement preparedStatement = db.getConnection().prepareStatement(sql);
		preparedStatement.setString(1, this.username);
		preparedStatement.setString(2, this.password);
		preparedStatement.setString(3,this.nickname);
		preparedStatement.setString(4, this.email==null?"":this.email);
		preparedStatement.setString(5, this.number==null?"":this.number);
		preparedStatement.setString(6, this.sex);
		
		
		
		
		//System.out.println(this.toString());
		int result =preparedStatement.executeUpdate();		
		db.closeService();
		if(preparedStatement.isClosed()==false){
		preparedStatement.close();
		}
		return result;
	}
	

}
